<!-- 标签管理 - 工程数据管理 - 标签数据源配置 -->
<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      :defaults.sync="defaults"
      :page.sync="page"
      :permission="permissionList"
      :before-close="beforeClose"
      v-model="form"
      ref="crud"
      @row-del="rowDel"
      @row-update="rowUpdate"
      @row-save="rowSave"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template slot="menu" slot-scope="{ row }">
        <el-button type="text" icon="el-icon-view" size="small" @click="handleViewDetail(row)">详情</el-button>
        <el-button type="text" icon="el-icon-edit" size="small" @click="handleEdit(row)">编辑</el-button>
      </template>
      <template slot="menuLeft" slot-scope="{ row }">
        <el-button type="primary" size="small" icon="el-icon-plus" @click="handleAdd"> 新增</el-button>
        <el-button type="danger" size="small" icon="el-icon-delete" plain @click="handleDelete">删 除 </el-button>
        <el-button type="success" size="small" icon="el-icon-success" @click="enable">启用 </el-button>
        <el-button type="danger" size="small" icon="el-icon-remove" @click="forbidden">禁用 </el-button>
      </template>
    </avue-crud>

    <!--新增编辑-->
    <edit-dialog :visible.sync="visibile" :dialog-type="dialogType" ref="editDialog" @closeDialog="onLoad(page)"></edit-dialog>
  </basic-container>
</template>

<script>
import { getPage, addSubmit, updateSubmit, deleteRemove, submitBatch, getShfitType } from '@/api/labelManagement/tagDataSource';
import { mapGetters } from 'vuex';
import userTableConfig from '@/components/user-table-config/index.vue';
import userConfig from '@/mixins/userConfig';
import editDialog from '@/views/labelManagement/components/editDialog';
export default {
  components: {
    userTableConfig,
    editDialog
  },
  // mixins: [userConfig()],
  data() {
    const self = this;
    return {
      shiftTypes: [], //关联数据字典，所有数据源
      dicDataValue: [], //
      lookStatus: true, // 查看状态
      disabledEdit: false, // 编辑禁选逻辑
      visibile: false,
      form: {},
      data: [],
      defaults: {},
      query: {},
      loading: true,
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      dialogType: '',
      selectionList: [],
      option: {
        height: 'calc(100vh - 360px)',
        tip: false,
        searchShow: true,
        searchSpan: 5,
        searchMenuSpan: 4,
        labelWidth: 110,
        dialogCustomClass: 'custom-class',
        border: true,
        index: true,
        indexLabel: '序号',
        addBtn: false,
        viewBtn: false,
        viewBtnText: '详情',
        viewTitle: '详情',
        editBtn: false,
        delBtn: true,
        refreshBtn: true,
        excelBtn: false,
        selection: true,
        dialogDrag: false, // 开启拖拽功能
        dialogClickModal: false,
        columnBtn: false,
        column: [
          {
            label: '数据名称',
            prop: 'name',
            search: true,
            maxlength: 30,
            rules: [
              {
                required: true,
                message: '请输入数据名称',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '数据编码',
            prop: 'code',
            rules: [
              {
                required: true,
                message: '请输入数据编码',
                trigger: 'blur'
              }
            ]
          },
          // {
          //   label: '数据字典',
          //   prop: 'type',
          //   type: 'select',
          //   dicUrl: '/api/blade-system/dict/dictionary?code=DATA_SQL_TYPE',
          //   props: {
          //     label: 'dictValue',
          //     value: 'dictKey'
          //   },
          //   rules: [
          //     {
          //       required: true,
          //       message: '请选择数据字典',
          //       trigger: 'blur'
          //     }
          //   ]
          // },
          {
            label: '数据描述',
            prop: 'remark',
            rules: [
              {
                required: true,
                message: '请输入数据描述',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '数据源类型',
            prop: 'type',
            searchLabelWidth: 100,
            type: 'select',
            dicData: [
              { label: '标签模板', value: 'LABEL_TEMPLATE' },
              { label: '序列号规则', value: 'SEQUENCE_DEFINITION' }
            ],
            search: true,
            rules: [{ required: true, message: '请选择数据源类型', trigger: 'change' }]
          },
          {
            label: '是否可用',
            search: true,
            prop: 'status',
            type: 'switch',
            value: 2,
            dataType: 'number',
            align: 'center',
            dicData: [
              {
                label: '否',
                value: 2
              },
              {
                label: '是',
                value: 1
              }
            ],
            slot: true,
            rules: [
              {
                required: true,
                message: '请选择是否可用',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '主SQL',
            prop: 'querySql',
            type: 'textarea',
            span: 24,
            maxRows: 6,
            minRows: 6,
            rules: [
              {
                required: true,
                message: '请输入主SQL',
                trigger: 'blur'
              }
            ]
          }
        ]
      }
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.$structuralSplit();
    });
  },
  computed: {
    ...mapGetters(['permission']),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.pallet_add, false),
        viewBtn: this.vaildData(this.permission.pallet_view, false),
        delBtn: this.vaildData(this.permission.pallet_delete, false),
        editBtn: this.vaildData(this.permission.pallet_edit, false)
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids.join(',');
    }
  },
  methods: {
    rowSave(row, done, loading) {
      addSubmit(row)
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '新增成功!'
          });
          done();
        })
        .catch((err) => {
          loading();
        });
    },

    rowUpdate(row, index, done, loading) {
      updateSubmit(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '编辑成功!'
          });
          done();
        },
        (error) => {
          loading();
        }
      );
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning('请选择至少一条数据');
        return;
      }
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          return deleteRemove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
          this.$refs.crud.toggleSelection();
        });
    },
    handleAdd() {
      this.visibile = true;
      this.dialogType = 'add';
    },
    async handleEdit(row) {
      await this.$refs.editDialog.getDetail(row.id);
      this.visibile = true;
      this.dialogType = 'add';
    },
    async handleViewDetail(row) {
      await this.$refs.editDialog.getDetail(row.id);
      this.visibile = true;
      this.dialogType = 'view';
    },
    //启用
    enable() {
      if (this.selectionList.length === 0) {
        this.$message.warning('请选择至少一条数据');
        return;
      }
      let typevalue = this.selectionList
        .filter((item) => item.status == 2)
        .map((item) => {
          return { id: item.id, status: 1 };
        });
      if (typevalue.length < this.selectionList.length) {
        this.$message.warning('数据不可重复启用');
        return;
      }
      this.$confirm('确定启用所选数据?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          return submitBatch(typevalue);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
          this.$refs.crud.toggleSelection();
        });
    },
    //禁用
    forbidden() {
      if (this.selectionList.length === 0) {
        this.$message.warning('请选择至少一条数据');
        return;
      }
      let typevalue = this.selectionList
        .filter((item) => item.status == 1)
        .map((item) => {
          return { id: item.id, status: 2 };
        });
      if (typevalue.length < this.selectionList.length) {
        this.$message.warning('数据不可重复启用');
        return;
      }
      this.$confirm('确定禁用所选数据?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          return submitBatch(typevalue);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeClose(done) {
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    rowDel(row) {
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          return deleteRemove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
        });
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getPage(page.currentPage, page.pageSize, Object.assign(params, this.query)).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    }
  }
};
</script>

<style lang="scss">
.custom-class {
  ::v-deep .avue-group__header .avue-group__title {
    font-weight: bold !important;
  }
}
</style>
